using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FaceCat;

namespace facecat
{
    /// <summary>
    /// 视图
    /// </summary>
    public class FCUIView : GraphicsView
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FCUIView()
        {
            onLoad();
            this.StartInteraction += FCUIView_StartInteraction;
            this.EndInteraction += FCUIView_EndInteraction;
            this.MoveHoverInteraction += FCUIView_MoveHoverInteraction;
        }

        /// <summary>
        /// 触摸移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FCUIView_MoveHoverInteraction(object? sender, TouchEventArgs e)
        {
            m_host.m_touchPoint = new FCPoint(e.Touches[0].X, e.Touches[0].Y);
            if (m_native.m_touchDownView != null)
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                newTouchInfo.m_clicks = 1;
                m_native.onMouseMove(newTouchInfo);
            }
            else
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                m_native.onMouseMove(newTouchInfo);
            }
        }

        public DateTime m_lastClickLeft;
        public bool m_isDoubleClickLeft;

        /// <summary>
        /// 触摸结束事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FCUIView_EndInteraction(object? sender, TouchEventArgs e)
        {
            m_host.m_touchPoint = new FCPoint(e.Touches[0].X, e.Touches[0].Y);
            if (m_host.allowOperate())
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                newTouchInfo.m_clicks = 1;
                if (m_isDoubleClickLeft)
                {
                    newTouchInfo.m_clicks = 2;
                    m_isDoubleClickLeft = false;
                }
                else
                {
                    newTouchInfo.m_clicks = 1;
                }
                m_native.onMouseUp(newTouchInfo);
            }
        }

        /// <summary>
        /// 触摸开始事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FCUIView_StartInteraction(object? sender, TouchEventArgs e)
        {
            m_host.m_touchPoint = new FCPoint(e.Touches[0].X, e.Touches[0].Y);
            if (m_host.allowOperate())
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                if (DateTime.Now - m_lastClickLeft < new TimeSpan(10000 * 300))
                {
                    newTouchInfo.m_clicks = 2;
                    m_isDoubleClickLeft = true;
                }
                else
                {
                    newTouchInfo.m_clicks = 1;
                }
                m_native.onMouseDown(newTouchInfo);
                m_lastClickLeft = DateTime.Now;
            }
        }

        /// <summary>
        /// 方法库
        /// </summary>
        public FCNative m_native;

        /// <summary>
        /// 设备对接
        /// </summary>
        public MauiHost m_host;

        /// <summary>
        /// Xml解析
        /// </summary>
        public FCUIXml m_xml;

        /// <summary>
        /// 加载数据
        /// </summary>
        public void onLoad()
        {
            m_xml = new FCUIXml();
            m_xml.createNative();
            m_native = m_xml.getNative();
            m_native.setPaint(new MauiPaint());
            m_host = new MauiHost();
            m_host.setView(this);
            m_native.setHost(m_host);
            m_host.setNative(m_native);
            m_native.setSize(new FCSize(800, 700));
            string xml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
  <html xmlns=""facecat"">
    <body>
      <div dock=""fill"" backcolor=""rgb(140,192,81)"">
        <div name=""Div"" text=""Div"" location=""368,127"" size=""247,326"" align=""center"" vertical-align=""middle"" backcolor=""rgb(170,178,189)"" cornerradius=""10"">
          <input type=""text"" name=""TextBox"" location=""14,18"" size=""220,45"" cornerradius=""0"" bordercolor=""rgb(0,0,0)"" backcolor=""rgb(42,48,72)"" font=""Default,30.0"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnAC"" text=""AC"" location=""14,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(62,72,95)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnAddSub"" text=""+/-"" location=""69,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(62,72,95)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnMod"" text=""%"" location=""124,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(62,72,95)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnDivide"" text=""÷"" location=""179,69"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum7"" text=""7"" location=""14,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum8"" text=""8"" location=""69,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum9"" text=""9"" location=""124,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnMulti"" text=""×"" location=""179,117"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum4"" text=""4"" location=""14,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum5"" text=""5"" location=""69,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum6"" text=""6"" location=""124,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnSub"" text=""-"" location=""179,165"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum1"" text=""1"" location=""14,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum2"" text=""2"" location=""69,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum3"" text=""3"" location=""124,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnAdd"" text=""+"" location=""179,213"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnNum0"" text=""0"" location=""14,261"" size=""110,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnDot"" text=""."" location=""124,261"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(93,103,121)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
          <input type=""button"" name=""btnResult"" text=""="" location=""179,261"" size=""55,48"" font=""Default,18.0"" backcolor=""rgb(254,159,11)"" bordercolor=""rgb(0,0,0)"" textcolor=""rgb(255,255,255)""/>
        </div>
      </div>
    </body>
  </html>";
            m_xml.loadXml(xml, null);
            m_native.update();
            GraphicsDrawable drawable = new GraphicsDrawable();
            Drawable = drawable;
            drawable.m_view = this;
        }
    }

    /// <summary>
    /// 重绘类
    /// </summary>
    public class GraphicsDrawable : IDrawable
    {
        /// <summary>
        /// 视图
        /// </summary>
        public FCUIView m_view;

        /// <summary>
        /// 绘制方法
        /// </summary>
        /// <param name="canvas"></param>
        /// <param name="dirtyRect"></param>
        public void Draw(ICanvas canvas, RectF dirtyRect)
        {
            int width = (int)m_view.Width, height = (int)m_view.Height;
            FCSize oldSize = m_view.m_native.getSize();
            if (oldSize.cx != width || oldSize.cy != height)
            {
                m_view.m_native.setSize(new FCSize(width, height));
                m_view.m_native.update();
            }
            MauiPaint mauiPaint = m_view.m_native.getPaint() as MauiPaint;
            mauiPaint.m_canvas = canvas;
            double scaleFactorX = 1, scaleFactorY = 1;
            mauiPaint.getScaleFactor(ref scaleFactorX, ref scaleFactorY);
            FCRect clipRect = new FCRect((int)(dirtyRect.Left * scaleFactorX), (int)(dirtyRect.Top * scaleFactorY), (int)(dirtyRect.Right * scaleFactorX), (int)(dirtyRect.Bottom * scaleFactorY));
            mauiPaint.beginPaint(0, clipRect, clipRect);
            m_view.m_native.onPaint(clipRect);
            mauiPaint.endPaint();
        }
    }
}
